﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//https://blog.csdn.net/qq_44713454/article/details/108834358
//https://blog.csdn.net/weixin_43919932/article/details/103327530
namespace CodeTop.复习九月份
{
    internal class SolPol
    {
        public string InorderToPol(string s)
        {
            Stack<char> stackNum = new Stack<char>();
            Stack<char> stackOpe = new Stack<char>();

            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (char.IsDigit(c))
                {
                    stackNum.Push(c);
                }
                else
                {
                    if (c == '(')
                    {
                        stackOpe.Push(c);
                    }
                    else if (c == ')')
                    {
                        while (stackOpe.Peek() != '(')
                        {
                            char curCh = stackOpe.Pop();

                            stackNum.Push(curCh);
                        }
                        stackOpe.Pop();//删除左括号'('
                    }
                    else//遇到符号了
                    {
                        while (stackOpe.Count > 0 && !HighLevel(c, stackOpe.Peek()))//栈顶元素大于c，出栈
                        {
                            char curCh = stackOpe.Pop();
                            stackNum.Push(curCh);
                        }
                        //栈顶元素小于c，才能入栈,栈空也可以入栈
                        stackOpe.Push(c);
                    }
                }
            }

            while (stackOpe.Count > 0)
            {
                char curCh = stackOpe.Pop();
                stackNum.Push(curCh);
            }

            char[] chars = stackNum.ToArray();
            Array.Reverse(chars);
            string res = "";
            for (int i = 0; i < chars.Length; i++)
            {
                Console.Write(chars[i] + ",");
                res += chars[i];
            }
            Console.WriteLine();

            return res;
        }

        private bool HighLevel(char cur, char top)//当前符号与栈顶符号比
        {
            //返回true就入栈，否则出栈
            if (top == '(')//一定进栈
            {
                return true;
            }
            if (cur == '/')
            {
                if (top == '+' || top == '-')
                {
                    return true;
                }
            }
            if (cur == '*')
            {
                if (top == '+' || top == '-')
                {
                    return true;
                }
            }
            return false;
        }
    }

    internal class ProgramPol
    {
        private static void MainPol(string[] args)
        {
            // string s = "2*((5-3)*4)-6/2";
            string s = "5+((1+2)*4)-3";
            SolPol solPol = new SolPol();
            string res = solPol.InorderToPol(s);
            Console.WriteLine(res);
        }
    }
}